package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmplyeeQueryObject;
import cn.wolfcode.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {

        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {

        //对密码进行加密
        Md5Hash md5Hash = new Md5Hash(employee.getPassword(), employee.getName());
        employee.setPassword(md5Hash.toString());


        //保存到数据库
        employeeMapper.insert(employee);
                                                                                            /*        if (ids != null && ids.length > 0){
                                                                                                        for (Long id : ids) {
                                                                                                                employeeMapper.insertRelation(employee.getId(),id);
                                                                                                        }
                                                                                                    }*/

        if (ids != null && ids.length > 0){
         employeeMapper.insertBatchRelation(employee.getId(),ids);
        }
    }

    @Override
    public Employee selectById(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);

        employeeMapper.deleteRelation(employee.getId());
        if (ids != null && ids.length > 0){
            employeeMapper.insertBatchRelation(employee.getId(),ids);
        }
    }

    @Override
    public PageInfo<Employee> query(EmplyeeQueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<>(employees);

    }

    @Override
    public Employee login(String username, String password) {
       Employee employee =  employeeMapper.selectByUsernameAndPassword(username,password);
        if (employee == null) {
            return null;
        }
        return employee;
    }

    @Override
    public Employee selectByPassword(String oldPassword,Long id) {
       Employee employee =  employeeMapper.selectByPassword(oldPassword,id);
        return employee;
    }

    @Override
    public void setPassword(Long id, String newPassword) {
        employeeMapper.setPassword(id,newPassword);
    }

    @Override
    public void bathcDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);
    }

    @Override
    public Employee selectByName(String name) {
        Employee employee = employeeMapper.selectByName(name);
        return employee;
    }

    @Override
    public Workbook exproXls() {
        //获取所有员工的数据
        List<Employee> employees = employeeMapper.selectAll();

        //创建一个excel对象
        Workbook workbook = new HSSFWorkbook();
        //创建一页纸
        Sheet sheet = workbook.createSheet("员工通讯录");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("员工名称");
        row.createCell(1).setCellValue("员工邮箱");
        row.createCell(2).setCellValue("员工年龄");

        for(int i = 0; i < employees.size();i++){
            Employee employee = employees.get(i);
            //创建一行
            row = sheet.createRow(i+1);
            //创建单元格 并写入数据
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            if (employee.getAge() != null){
                row.createCell(2).setCellValue(employee.getAge());
            }
            //创建单元格
            //Cell cell = row.createCell(3);
            //在单元格上写东西
            //cell.setCellValue("hello");
        }
        return workbook;
    }

    @Override
    public void imporXls(MultipartFile file) throws IOException {
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++){
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            String name = row.getCell(0).getStringCellValue();
            String email = row.getCell(1).getStringCellValue();
            //获取指定的单元格的类型
            CellType cellType = row.getCell(2).getCellType();
            //判断单元格的类型是否是string
            if (cellType.equals(CellType.STRING)){
                String age = row.getCell(2).getStringCellValue();
                employee.setAge(Integer.valueOf(age));
            }else{
                double value = row.getCell(2).getNumericCellValue();
                employee.setAge((int)value);
            }
            employee.setName(name);
            employee.setEmail(email);
            employee.setPassword("1");
            this.save(employee,null);
        }














  /*      //把接收到的文件以excel的方式去读取并操作
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //读取第一页
        Sheet sheet = wb.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        //从索引为1的行数开始读(忽略标题行)
        for (int i = 1; i <= lastRowNum; i++) {
            //获取行数据
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            //判断如果用户名是空，就不再往下读
            if(!StringUtils.hasLength(name.trim())){
                return;
            }
            Employee employee = new Employee();
            employee.setName(name);
            employee.setEmail(row.getCell(1).getStringCellValue());
            //获取文本格式的单元格内容
            employee.setAge(Integer.valueOf(row.getCell(2).getStringCellValue()));
            //获取数值类型的单元格内容
            //double value = row.getCell(2).getNumericCellValue();
            //employee.setAge((int)value);
            //设置默认密码1
            employee.setPassword("1");
            //调用保存方法
            this.save(employee,null);
        }*/
    }

    //根据角色编码查询对应的员工
    @Override
    public List<Employee> selectByRoleSn(String... sns) {
        return employeeMapper.selectByRoleSn(sns);
    }
}
